//
// See the file COPYING for redistribution information.
//

#include <config.h>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <Naipax/Common.hpp>
#include <Naipax/Version.hpp>
#include <Naipax/Error.hpp>
#include <Naipax/Config.hpp>

using namespace std;
using namespace Naipax;

static const cfg_opt_t sDefaultOpts[] = {
    CFG_STR("UrlPool", "url_pool", CFGF_NONE),
    CFG_STR("UrlHistoryFile", "url_history", CFGF_NONE),
    CFG_STR("CrawlerAgent", "Naipax/" NAIPAX_VERSION, CFGF_NONE),
    CFG_STR("CrawlerEmail", "", CFGF_NONE),
    CFG_INT("CrawlerConnections", 300, CFGF_NONE),
    CFG_INT("ConnectionTimeout", 30, CFGF_NONE),
    CFG_INT("MaxBandwidth", -1, CFGF_NONE),
    CFG_BOOL("CrawlExternalLinks", cfg_true, CFGF_NONE),
    CFG_INT("MaxUrlDepth", 5, CFGF_NONE),
    CFG_STR("LogPath", "log", CFGF_NONE),
    CFG_BOOL("UseCookies", cfg_false, CFGF_NONE),
    CFG_INT("ServicePort", 1997, CFGF_NONE),
    CFG_STR("AllowDomain", "", CFGF_NONE),
    CFG_STR("DisallowDomain", "", CFGF_NONE),
    CFG_STR("AllowUrl", "", CFGF_NONE),
    CFG_STR("DisallowUrl", "", CFGF_NONE),
    CFG_STR("AllowContentType", "", CFGF_NONE),
    CFG_STR("DisallowContentType", "", CFGF_NONE),
    CFG_END()
};

void Config::_ConvertOpts() {
    mUrlPool = cfg_getstr(_mpConfuse, "UrlPool");
    mUrlHistoryFile = cfg_getstr(_mpConfuse, "UrlHistoryFile");
    mCrawlerAgent = cfg_getstr(_mpConfuse, "CrawlerAgent");
    mCrawlerEmail = cfg_getstr(_mpConfuse, "CrawlerEmail");
    mCrawlerConnections = cfg_getint(_mpConfuse, "CrawlerConnections");
    mConnectionTimeout = cfg_getint(_mpConfuse, "ConnectionTimeout");
    mMaxBandwidth = cfg_getint(_mpConfuse, "MaxBandwidth");
    mCrawlExternalLinks
        = cfg_getbool(_mpConfuse,
                      "CrawlExternalLinks") == cfg_true ? true : false;
    mMaxUrlDepth = cfg_getint(_mpConfuse, "MaxUrlDepth");
    mLogPath = cfg_getstr(_mpConfuse, "LogPath");
    mUseCookies
        = cfg_getbool(_mpConfuse,
                      "UseCookies") == cfg_true ? true : false;
    mServicePort = cfg_getint(_mpConfuse, "ServicePort");
    mAllowDomain = cfg_getstr(_mpConfuse, "AllowDomain");
    mDisallowDomain = cfg_getstr(_mpConfuse, "DisallowDomain");
    mAllowUrl = cfg_getstr(_mpConfuse, "AllowUrl");
    mDisallowUrl = cfg_getstr(_mpConfuse, "DisallowUrl");
    mAllowContentType = cfg_getstr(_mpConfuse, "AllowContentType");
    mDisallowContentType = cfg_getstr(_mpConfuse, "DisallowContentType");
}

Config::Config() {
    _mpConfuse = cfg_init((cfg_opt_t*) sDefaultOpts, CFGF_NONE);
    if (_mpConfuse == NULL) {
        NAIPAX_THROW(Error::CANNOT_INIT_CONFIG);
    }
}
        
Config::~Config() {
    cfg_free(_mpConfuse);
}

void Config::ParseFile(const char *pFilename) {
    if (pFilename == NULL) {
        NAIPAX_THROW(Error::INVALID_ARGUMENT);
    }

    if(cfg_parse(_mpConfuse, pFilename) == CFG_PARSE_ERROR) {
        NAIPAX_THROW(Error::CANNOT_PARSE_FILE);
    }
    _ConvertOpts();
}

void Config::Dump(string &rStr) {
    stringstream Ss;
    Ss << "UrlPool: " << mUrlPool << endl
       << "UrlHistoryFile: " << mUrlHistoryFile << endl
       << "CrawlerAgent: " << mCrawlerAgent << endl
       << "CrawlerEmail: " << mCrawlerEmail << endl
       << "CrawlerConnections: " << mCrawlerConnections << endl
       << "ConnectionTimeout: " << mConnectionTimeout << endl
       << "MaxBandwidth: " << mMaxBandwidth << endl
       << "CrawlExternalLinks: " << mCrawlExternalLinks << endl
       << "MaxUrlDepth: " << mMaxUrlDepth << endl
       << "LogPath: " << mLogPath << endl
       << "UseCookies: " << mUseCookies << endl
       << "ServicePort: " << mServicePort << endl
       << "AllowDomain: " << mAllowDomain << endl
       << "DisallowDomain: " << mDisallowDomain << endl
       << "AllowUrl: " << mAllowUrl << endl
       << "DisallowUrl: " << mDisallowUrl << endl
       << "AllowContentType: " << mAllowContentType << endl
       << "DisallowContentType: " << mDisallowContentType << endl;
    rStr = Ss.str();
}
